﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class AdaugareFunctiiVitale : UserControl
    {
        private Control redControl;
        string CNP;
        string limba;

        public AdaugareFunctiiVitale(string limba, string CNP)
        {
            InitializeComponent();
            Rename(limba);
            this.CNP = CNP;
            this.limba = limba;
        }

        private void btAdaugaFunctiiVitale_Click(object sender, EventArgs e)
        {
            if (redControl != null)
                changeColor(redControl);

            List<Control> toValidate = new List<Control>();
            bool isValid = true;

            // Validare AdaugareFunctiiVitale;
            toValidate.Add(tbGreutate);
            toValidate.Add(tbTensiuneArteriala);
            toValidate.Add(tbPuls);
            toValidate.Add(tbFreceventaRespiratorie);
            toValidate.Add(tbApetit);
            toValidate.Add(tbDiureza);
            toValidate.Add(tbTranzit);
            toValidate.Add(tbConcluziiFunctiiVitale);
            toValidate.Add(dtDataExternarii);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

            foreach (Control tb in toValidate)
            {
                Validation val = new Validation(tb, tb.Text);
                isValid = val.validate();

                if (isValid == false)
                {
                    string eroare = val.getError();
                    redControl = tb;
                    MessageBox.Show(errors["CampIncorect"], "Error",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            if (isValid == true)
            {
                FunctiiVitale fv = new FunctiiVitale();

                fv.IdPacient = CNP;
                fv.Greutate = Int32.Parse(tbGreutate.Text);
                fv.TensiuneArteriala = Int32.Parse(tbTensiuneArteriala.Text);
                fv.Puls = Int32.Parse(tbPuls.Text);
                fv.FrecventaRespiratorie = Int32.Parse(tbFreceventaRespiratorie.Text);
                fv.Apetit = tbApetit.Text;
                fv.Diureza = tbDiureza.Text;
                fv.TranzitIntestinal = tbTranzit.Text;
                fv.ConcluziiAsistent = tbConcluziiFunctiiVitale.Text;
                fv.DataEvaluarii = dtDataExternarii.Value;
                
                //salvare in BD
                if (isValid == true)
                {
                    try
                    {
                        PersistenceManager pm = PersistenceManager.Instance;

                        pm.Save(fv);
                        MessageBox.Show(errors["okBD"], "Information",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    {
                        MessageBox.Show(errors["ErrorBD"], "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void changeColor(Control cont)
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
        }
    }
}
